local skel = fk.CreateSkill {
  name = "rmt__tuicuan",
}

skel:addEffect(fk.TargetSpecified, {
  anim_type = "control",
  can_trigger = function(self, event, target, player, data)
    if target == player and player:hasSkill(skel.name) then
      return data.firstTarget
    end
  end,
  on_cost = function (self, event, target, player, data)
    local room = player.room
    local tos = table.filter(data.use.tos, function (p) return not p.dead end)
    if #tos == 0 then return false end
    if #tos == 1 then
      if room:askToSkillInvoke(player, { skill_name = skel.name, prompt = "#rmt__tuicuan-invoke:"..tos[1].id}) then
        event:setCostData(self, {tos = tos})
        return true
      end
    else
      tos = room:askToChoosePlayers(player, {
        max_num = 1, min_num = 1, skill_name = skel.name, prompt = "#rmt__tuicuan-choose", targets = tos,
      })
      if #tos > 0 then
        event:setCostData(self, {tos = tos})
        return true
      end
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local to = event:getCostData(self).tos[1]
    if player.phase ~= Player.NotActive then
      room:addPlayerMark(to, "@rmt__tuicuan-turn", 1)
    end
    to:drawCards(1, skel.name)
  end,
})

skel:addEffect(fk.EventPhaseStart, {
  anim_type = "control",
  can_trigger = function(self, event, target, player, data)
    if target == player and player:hasSkill(skel.name) then
      return player.phase == Player.Finish and table.find(player.room.alive_players, function (p)
        return p:getMark("@rmt__tuicuan-turn") ~= 0
      end)
    end
  end,
  on_cost = function (self, event, target, player, data)
    local room = player.room
    local maxNum = 0
    for _, p in ipairs(room.alive_players) do
      maxNum = math.max(maxNum, p:getMark("@rmt__tuicuan-turn"))
    end
    local tos = table.filter(room.alive_players, function (p)
      return p:getMark("@rmt__tuicuan-turn") == maxNum
    end)
    if #tos == 0 then return false end
    if #tos > 1 then
      tos = room:askToChoosePlayers(player, {
        targets = tos, max_num = 1, min_num = 1, cancelable = false, skill_name = skel.name,
        prompt = "#rmt__tuicuan-throw:::"..(maxNum+1)
      })
    end
    event:setCostData(self, {tos = tos, num = maxNum + 1 })
    return true
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local to = event:getCostData(self).tos[1]
    if to:isNude() then return end
    local cards = to:getCardIds("he")
    local num = event:getCostData(self).num
    local suit = room:askToChoice(player, {
      skill_name = skel.name, choices = {"log_spade", "log_heart", "log_club", "log_diamond"},
      prompt = "#rmt__tuicuan-suit::"..to.id..":"..num,
    })
    if #cards > num then
      cards = room:askToChooseCards(player, {target = to, max = num, min = num, flag = "he", skill_name = skel.name})
    end
    room:throwCard(cards, skel.name, to, player)
    if player.dead then return end
    cards = table.filter(cards, function (id)
      return room:getCardArea(id) == Card.DiscardPile and Fk:getCardById(id):getSuitString(true) == suit
    end)
    if #cards > 0 then
      room:delay(500)
      room:obtainCard(player, cards, true, fk.ReasonJustMove, player, skel.name)
    end
  end,
})

Fk:loadTranslationTable{
  ["rmt__tuicuan"] = "推篡",
  [":rmt__tuicuan"] = "你使用牌指定目标后，可以令一名目标角色摸一张牌；结束阶段，你选择一种花色，弃置本回合以此法摸牌最多的一名角色等量+1张牌，获得其中所选花色的牌。",
  ["@rmt__tuicuan-turn"] = "推篡",
  ["#rmt__tuicuan-choose"] = "推篡：你可以令一名目标角色摸一张牌！",
  ["#rmt__tuicuan-invoke"] = "推篡：你可以令 %src 摸一张牌！",
  ["#rmt__tuicuan-throw"] = "推篡：选择一名角色，弃置其 %arg 张牌",
  ["#rmt__tuicuan-suit"] = "推篡：你即将弃置 %dest %arg张牌，选择你要获得的花色！",

  ["$rmt__tuicuan1"] = "君薨而世子立，臣安敢乱之？",
  ["$rmt__tuicuan2"] = "兽者，朕也!",
}

return skel
